package com.quanyan.scheduler.web;


import com.quanyan.api.APIResponse;
import com.quanyan.scheduler.cache.api.redis.JedisCacheApi;
import com.quanyan.scheduler.entity.db.TbWeichatAccount;
import com.quanyan.scheduler.mapper.TbWeichatAccountMapper;
import com.quanyan.weichat.api.WeichatServiceApi;
import com.quanyan.weichat.common.RandomStringGenerator;
import com.quanyan.weichat.common.SHA1;
import com.quanyan.weichat.protocol.common_protocal.WeichatJSConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chuntu on 2015/7/2.
 */
@RestController
@RequestMapping(value = "/weixin/")
public class WeichatController {
    private static Logger log = LoggerFactory.getLogger(WeichatController.class);
    private static String[] jsApiList = new String[]{"onMenuShareTimeline",
            "onMenuShareAppMessage",
            "onMenuShareQQ",
            "onMenuShareWeibo",
            "startRecord",
            "stopRecord",
            "onVoiceRecordEnd",
            "playVoice",
            "pauseVoice",
            "stopVoice",
            "onVoicePlayEnd",
            "uploadVoice",
            "downloadVoice",
            "chooseImage",
            "previewImage",
            "uploadImage",
            "downloadImage",
            "translateVoice",
            "getNetworkType",
            "openLocation",
            "getLocation",
            "hideOptionMenu",
            "showOptionMenu",
            "hideMenuItems",
            "showMenuItems",
            "hideAllNonBaseMenuItem",
            "showAllNonBaseMenuItem",
            "closeWindow",
            "scanQRCode",
            "chooseWXPay",
            "openProductSpecificView",
            "addCard",
            "chooseCard",
            "openCard"};
    @Autowired
    TbWeichatAccountMapper tbWeichatAccountMapper;
    @Autowired
    private JedisCacheApi jedisCacheApi;

    /**
     * 获取微信jsApi配置属性.
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "wxConfig")
    @ResponseBody
    public APIResponse<WeichatJSConfig> wxConfig(@RequestParam("wxId") Integer wxId, @RequestParam("url") String url, HttpServletRequest request) {
        log.info("method-wxConfig:" + "wxId-" + wxId);
        WeichatJSConfig jsConfig = new WeichatJSConfig(true, jsApiList, url);
        WeichatServiceApi weichatServiceApi = new WeichatServiceApi();
        TbWeichatAccount tbWeichatAccount = tbWeichatAccountMapper.selectByPrimaryKey(wxId);

        String appId = tbWeichatAccount.getAppId();
        String securityKey = tbWeichatAccount.getSecurityKey();
        log.warn("method-wxConfig:" + "appId-" + appId + "  securityKey-" + securityKey);
        String access_token = jedisCacheApi.hget("token_" + appId, "access_token");
        String ticket = jedisCacheApi.hget("ticket_" + appId, "ticket");

        log.warn("access_token>>>>>>>>>>>>>:" + access_token);
        log.warn("ticket>>>>>>>>>>>>>:" + ticket);

        String nonceStr = RandomStringGenerator.getRandomStringByLength(32);
        String timestamp = "" + (System.currentTimeMillis() / 1000);
        jsConfig.setTimestamp(timestamp);
        jsConfig.setNonceStr(nonceStr);
        jsConfig.setAppId(appId);
        String string = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url=" + url;
        System.out.println("======ticket:"+ticket+" nonceStr:"+nonceStr+" timestamp:"+timestamp+" url:"+url);
        jsConfig.setSignature(new SHA1().getDigestOfString(string.getBytes()).toLowerCase());
        return APIResponse.returnSuccess(jsConfig);
    }


}
